package com.hundsun.documentripper.service.impl;

import com.hundsun.documentripper.domain.dto.FileNodeView;
import com.hundsun.documentripper.domain.dto.FileView;
import com.hundsun.documentripper.domain.pojo.File;
import com.hundsun.documentripper.domain.pojo.FileNode;
import com.hundsun.documentripper.mapper.FileMapper;
import com.hundsun.documentripper.service.FileNodeService;
import com.hundsun.documentripper.service.FileViewService;
import com.hundsun.documentripper.service.NodeContentService;
import com.hundsun.documentripper.service.NodeTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Note：
 *
 * @author ：mengjw
 * @description：文件展示接口实现
 * @date ：Created in 2021/2/1
 */
@Service
public class FileViewServiceImpl implements FileViewService {

    @Autowired
    private FileMapper fileMapper;
    @Autowired
    private FileNodeService fileNodeService;
    @Autowired
    private NodeTemplateService nodeTemplateService;
    @Autowired
    private NodeContentService nodeContentService;

    @Override
    @Deprecated
    public FileView getFileViewById(String uuid) {
        File file = fileMapper.selectById(uuid);
        // 根据文件编号获取节点列表
        List<FileNode> fileNodeList = fileNodeService.getFileNodeListById(uuid);
        List<FileNodeView> fileNodeViewList = new ArrayList<>();
        for (FileNode fileNode : fileNodeList) {
            FileNodeView fileNodeView = FileNodeView.fromFileNode(fileNode);
            fileNodeView.setFileNodeTemplate(nodeTemplateService.getNodeTemplateView(fileNode.getFileNodeTemplateUuid()));
            fileNodeView.setNodeContent(nodeContentService.getNodeContent(fileNode.getContentUuid()));
            fileNodeView.setContentNodeTemplate(nodeTemplateService.getNodeTemplateView(fileNode.getContentNodeTemplateUuid()));
            // 根据模板获取模板的域
            fileNodeViewList.add(fileNodeView);
        }
        FileView fileView = FileView.formFile(file);
        fileView.setFileNodeViews(fileNodeViewList);
        return fileView;
    }

    @Override
    @Deprecated
    public FileView saveFileView(FileView fileView) {
        return null;
    }

}